package uk.co.q3c.deplan.client.ui.view;

import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import uk.co.q3c.deplan.client.Orchestrator;
import uk.co.q3c.deplan.client.ui.mock.view.ScreenViewMock;
import uk.co.q3c.deplan.client.ui.mock.view.ViewCreatorMock;
import uk.co.q3c.deplan.client.ui.presenter.HomePresenter;
import uk.co.q3c.deplan.client.ui.presenter.Presenter;
import uk.co.q3c.deplan.client.ui.presenter.ProjectsPresenter;
import uk.co.q3c.deplan.client.ui.presenter.ResourcesPresenter;

@Test
public class ScreenView_UT {
	transient protected final Logger logger = Logger.getLogger(getClass().getName());
	private ScreenView screenView;
	private ScreenPresenter presenter;
	private ViewCreatorMock vc;

	@BeforeClass
	protected void beforeClass() {
		screenView = new ScreenViewMock();
		Orchestrator.setScreenView(screenView);
		presenter = screenView.getPresenter();
		vc = new ViewCreatorMock();
		Orchestrator.setViewCreator(vc);
	}

	/**
	 * switch views from the menu items, checking that the right presenter is
	 * also present. Second iteration also checks that the view is a cached
	 * version (it is the same instance as the first time round) but that the
	 * presenter is a new instance
	 */
	public void switchViewsfromMenu() {
		Q3CoreView activeView;

		Q3CoreView resourcesView1;
		Q3CoreView projectsView1;
		Q3CoreView homeView1;

		Presenter resourcesPresenter1;
		Presenter projectsPresenter1;
		Presenter homePresenter1;

		Q3CoreView resourcesView2;
		Q3CoreView projectsView2;
		Q3CoreView homeView2;

		Presenter resourcesPresenter2;
		Presenter projectsPresenter2;
		Presenter homePresenter2;

		presenter.cmdOpenResourcesView.execute();
		activeView = presenter.getCurrentCoreView();
		resourcesView1 = activeView;
		Assert.assertEquals(activeView.viewTag(), Viewtags.ResourcesView);
		Assert.assertNotNull(activeView.getPresenter());
		resourcesPresenter1 = activeView.getPresenter();
		Assert.assertTrue(activeView.getPresenter()
				.getClass() == ResourcesPresenter.class);

		presenter.cmdOpenProjectsView.execute();
		activeView = presenter.getCurrentCoreView();
		projectsView1 = activeView;
		Assert.assertEquals(activeView.viewTag(), Viewtags.ProjectsView);
		Assert.assertNotNull(activeView.getPresenter());
		projectsPresenter1 = activeView.getPresenter();
		Assert.assertTrue(activeView.getPresenter()
				.getClass() == ProjectsPresenter.class);

		presenter.cmdOpenHomeView.execute();
		activeView = presenter.getCurrentCoreView();
		homeView1 = activeView;
		Assert.assertEquals(activeView.viewTag(), Viewtags.HomeView);
		Assert.assertNotNull(activeView.getPresenter());
		Assert.assertTrue(activeView.getPresenter()
				.getClass() == HomePresenter.class);

		// second time round, look for cached views
		presenter.cmdOpenResourcesView.execute();
		activeView = presenter.getCurrentCoreView();
		resourcesView2 = activeView;
		Assert.assertEquals(activeView.viewTag(), Viewtags.ResourcesView);
		Assert.assertNotNull(activeView.getPresenter());
		resourcesPresenter2 = activeView.getPresenter();
		Assert.assertTrue(activeView.getPresenter()
				.getClass() == ResourcesPresenter.class);
		Assert.assertEquals(resourcesView1, resourcesView2);
		Assert.assertNotSame(resourcesPresenter1, resourcesPresenter2);

		presenter.cmdOpenProjectsView.execute();
		activeView = presenter.getCurrentCoreView();
		projectsView2 = activeView;
		Assert.assertEquals(activeView.viewTag(), Viewtags.ProjectsView);
		Assert.assertNotNull(activeView.getPresenter());
		projectsPresenter2 = activeView.getPresenter();
		Assert.assertTrue(activeView.getPresenter()
				.getClass() == ProjectsPresenter.class);
		Assert.assertEquals(projectsView1, projectsView2);
		Assert.assertNotSame(projectsPresenter1, projectsPresenter2);
		logger.info(vc.history);
	}
}
